/**
 * Created by meiguizhinian on 2018/7/20
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//cocos2dx class
ivy.UISkillButton = ivy.UIBase.extend({
	ctor: function (formName, blockName, sourceControlData) {
		this._super();

		this._iconAniID = new ivy.AnimationID();
		this._showData = new ivy.UISkillButton.UISkillShowData();
		this._showDataBak = new ivy.UISkillButton.UISkillShowData();
		if (formName && blockName && sourceControlData) {
			this.initWith(formName, blockName, sourceControlData);
		}
	},

	/**
	 * initWith(int32_t formID, int32_t blockID, std::string cdFntName, TouchMode touchMode = TouchMode::eMulitTouch);
	 * initWith(const std::string& aniFileName, int32_t actionID, const cc::AnimationID& iconAniID, const cc::AnimationID& maskAniID, cdFntName, touchMode = TouchMode::eMulitTouch);
	 * initWith(formName, blockName, sourceControlData)
	 * */
	initWith : function() {
		this.setCascadeColorAndOpacityEnabled(true);
		if (cc.isNumber(arguments[0])) {
			let formID = arguments[0];
			let blockID = arguments[1];
			let cdFntName = arguments[2];
			let touchMode = arguments.length>3 ? arguments[3]:ivy.UIBase.TouchMode.eMulitTouch;


			this._buttonBase = ivy.UIButton.create();
			this._buttonBase.initWith(formID, blockID, touchMode);
			this.addChild(this._buttonBase);
			if (this._iconAniID.vaild()) {
				this._iconPlayer = ivy.AniPlayer.create(this._iconAniID);
				this._buttonBase.addChild(this._iconPlayer);
			}
			let uim = ivy.UIManager.getInstance();
			let aniIDInUI = uim.getBlockDataByType(formID, blockID, ivy.UIManager.FormDataType.eSelBgAniID);
			let actionIDInUI = uim.getBlockDataByType(formID, blockID, ivy.UIManager.FormDataType.eSelBgActionID);
			let seqIDInUI = uim.getBlockDataByType(formID, blockID, ivy.UIManager.FormDataType.eSelBgSeqID);
			if (aniIDInUI >= 0) {
				let aniFileName = uim.getAniFileNameByUIAniID(aniIDInUI);
				this._maskPlayer = ivy.AniPlayer.create(aniFileName, actionIDInUI, seqIDInUI === -1 ? 0 : seqIDInUI, true, seqIDInUI === -1 ? ivy.AniPlayState.ePause : ivy.AniPlayState.eCircle);
				this._maskPlayer.setVisible(false);
				this._buttonBase.addChild(this._maskPlayer, 1);
			}
			this._cdLabel = cc.Label.createWithBMFont(cdFntName, "");
			this._buttonBase.addChild(this._cdLabel, 1);
			this._cdLabel.setVisible(false);
			this.scheduleUpdate();
		} else {
			if (cc.isNumber(arguments[1])) {
				let aniFileName = arguments[0];
				let actionID = arguments[1];
				let iconAniID = arguments[2];
				let maskAniID = arguments[3];
				let cdFntName = arguments[4];
				let touchMode = arguments.length>5 ? arguments[5]:ivy.UIBase.TouchMode.eMulitTouch;

				this._buttonBase = ivy.UIButton.create();
				this._buttonBase.initWith(aniFileName, actionID, ivy.AniPlayState.ePause, touchMode);
				this.addChild(this._buttonBase);
				this._iconAniID = iconAniID;
				if (this._iconAniID.vaild())
				{
					this._iconPlayer = ivy.AniPlayer.create(iconAniID);
					this._buttonBase.addChild(this._iconPlayer);
				}
				this._maskPlayer = ivy.AniPlayer.create(maskAniID);
				this._maskPlayer.setVisible(false);
				this._buttonBase.addChild(this._maskPlayer, 1);
				this._cdLabel = cc.Label.createWithBMFont(cdFntName, "");
				this._buttonBase.addChild(this._cdLabel, 1);
				this._cdLabel.setVisible(false);
				this.scheduleUpdate();
			} else {
				let formName = arguments[0];
				let blockName = arguments[1];
				let sourceControlData = arguments[2];

				let uim = ivy.UIManager.getInstance();
				this._buttonBase = ivy.UIButton.create();
				this._buttonBase.initWith(formName,blockName,sourceControlData);
				this.addChild(this._buttonBase);

				let aniIDInUISel = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eSelBgAniID);
				let actionIDInUISel = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eSelBgActionID);
				let seqIDInUISel = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eSelBgSeqID);
				let x = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.ePosX);
				let y = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.ePosY);
				let w = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eWidth);
				let h = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eHeight);
				let layerID = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eLayerID);
				let seqIDInUI = 0;//TODO
				this._layerIDInEditor = layerID;

				let aniFileNameMask = ivy.UIManager.getInstance().getAniFileNameByUIAniID(aniIDInUISel);
				if (aniFileNameMask) {
					this._maskPlayer = ivy.AniPlayer.create(aniFileNameMask, actionIDInUISel, seqIDInUISel === -1 ? 0 : seqIDInUI, true, seqIDInUI === -1 ? ivy.AniPlayState.ePause : ivy.AniPlayState.eCircle);
					if(this._maskPlayer) {
						this._buttonBase.addChild(this._maskPlayer, 1);
						this._maskPlayer.setVisible(false);
					}
				}
				this._buttonBase.setPosition(cc.p());

				let cdFntName = uim.getControlStringDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_SkillAniButton_FNTPath_string]);
				let fntSize= uim.getControlIntDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_SkillAniButton_Size_int]);
				let color4b= ivy.Tools.getColor4BFromInt32( uim.getControlColorDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_SkillAniButton_Color_color]));
				let bindSkillIndex = uim.getControlIntDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_SkillAniButton_BindSkillIndex_int]);
				this._bindSkillIndex = bindSkillIndex;

				this._cdLabel = cc.Label.createWithBMFont(cdFntName, "");
				this._cdLabel.setColor(cc.color(color4b));
				this._cdLabel.setOpacity(color4b.a);
				this._buttonBase.addChild(this._cdLabel, 1);
				this._cdLabel.setVisible(false);
				this.scheduleUpdate();

				this.setSrcRect(cc.rect(x, y, w, h));
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(x + w / 2.0, y + h / 2.0)));
				this.setSrcPosition(this.getPosition());
				this.setContentSize(cc.size(w, h));
				let anchorType = uim.getControlIntDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_SkillAniButton_Anchor_list]);
				this.setAnchorType(anchorType);
				this._enterActionID.setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_SkillAniButton_EnterAction_action]));
				this._outActionID.setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_SkillAniButton_OutAction_action]));
				this.setUIElementName(formName, blockName);
			}
		}
	},

	setRefreshFunc : function(func) {
		this._refreshShowDataFunc = func;
	},
	setTouchEnterCallback : function(func) {
		if (this._buttonBase)
			this._buttonBase.setTouchEnterCallback(func);
	},
	setTouchingCallback : function(func) {
		if (this._buttonBase)
			this._buttonBase.setTouchingCallback(func);
	},
	setTouchEndCallback : function(func) {
		if (this._buttonBase)
			this._buttonBase.setTouchEndCallback(func);
	},
	setTouchDoubleCallback : function(func) {
		if (this._buttonBase)
			this._buttonBase.setTouchDoubleCallback(func);
	},
	setTouchCancelCallback : function(func) {
		if (this._buttonBase)
			this._buttonBase.setTouchCancelCallback(func);
	},
	setTouchEnable : function(enable) {
		if (this._buttonBase)
			this._buttonBase.setTouchEnable(enable);
	},
	setEnableUserTouch : function(enable) {
		if (this._buttonBase)
			this._buttonBase.setEnableUserTouch(enable);
	},
	setContentSize : function(size) {
		this._super(size);
		this._buttonBase.setContentSize(size);
	},
	setVisible : function(visible) {
		this._super(visible);
		if (this._buttonBase)
			this._buttonBase.setVisible(visible);
	},
	getButtonBase : function() {
		return this._buttonBase;
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////protected:
	update : function() {
		if (this._refreshShowDataFunc)
			this._refreshShowDataFunc(this, this._showData, this._bindSkillIndex);
		if (this._showData.iconAni.vaild() && !this._iconAniID.equal(this._showData.iconAni)) {
			this._iconAniID = this._showData.iconAni;
			if (!this._iconPlayer) {
				this._iconPlayer = ivy.AniPlayer.create(this._iconAniID);
				this._iconPlayer.retain();
				ivy.Tools.pushFunctionToCocosThread(function() {
					this._buttonBase.addChild(this._iconPlayer);
					this._iconPlayer.release();
				}.bind(this));
			}
		}
		if (this._showData.enable !== this._showDataBak.enable) {
			this._showDataBak.enable = this._showData.enable;
			if(this._maskPlayer)
				this._maskPlayer.setVisible(!this._showDataBak.enable);
		}
		if (this._showData.cdTime !== this._showDataBak.cdTime) {
			this._showDataBak.cdTime = this._showData.cdTime;
			if (this._cdLabel) {
				this._cdLabel.setString(this._showData.cdTime.toString());
				if (this._showData.cdTime > 0)
					this._cdLabel.setVisible(true);
				else {
					this._cdLabel.setVisible(false);
				}
			}
		}
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////private:
	_iconAniID : null,//ivy.AnimationID
	_buttonBase : null,//UIButton*
	_iconPlayer : null,//ivy::AniPlayer*
	_maskPlayer : null,//ivy::AniPlayer*
	_cdLabel : null,//cc.Label*
	_showData : null,//UISkillShowData
	_showDataBak : null,//UISkillShowData
	_bindSkillIndex : -1,
	_refreshShowDataFunc : null,
});

ivy.UISkillButton.create = function (formName, blockName, sourceControlData) {
	return new ivy.UISkillButton(formName, blockName, sourceControlData);
};

(function(){
	ivy.UISkillButton.UISkillShowData = function (param) {
		//成员变量
		this.iconAni = new ivy.AnimationID();
		this.enable = false;
		this.cdTime = 0;
	};
})();